<template>
  <Menu
    default-active="1-4-1"
    class="Menu-vertical-demo"
    @open="handleOpen"
    @close="handleClose"
    :collapse="isCollapse"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
  >
    <h3>{{ isCollapse ? "后台" : "通用后台管理系统" }}</h3>
    <MenuItem
      @click="clickMenu(item)"
      v-for="item in noChildren"
      :key="item.name"
      :index="item.path"
    >
      <i :class="`el-icon-${item.icon}`"></i>
      <span slot="title">{{ item.label }}</span>
    </MenuItem>
    <Submenu v-for="item in hasChildren" :key="item.label" :index="item.label">
      <template slot="title">
        <i :class="`el-icon-${item.icon}`"></i>
        <span slot="title">{{ item.label }}</span>
      </template>
      <MenuItemGroup v-for="subItem in item.children" :key="subItem.path">
        <MenuItem @click="clickMenu(subItem)" :index="subItem.path">{{
          subItem.label
        }}</MenuItem>
      </MenuItemGroup>
    </Submenu>
  </Menu>
</template>

<style lang="less" scoped>
.Menu-vertical-demo:not(.Menu--collapse) {
  // width: 200px;
  min-height: 100vh;

  height: 100%;
  border-right: none;
  h3 {
    color: #fff;
    text-align: center;
    line-height: 48px;
    font-size: 16px;
    font-weight: 400px;
  }
}
</style>

<script>
import Cookie from "js-cookie";
import { Menu, MenuItem, Submenu, MenuItemGroup } from "element-ui";
export default {
  data() {
    return {};
  },
  components: {
    Menu,
    MenuItem,
    Submenu,
    MenuItemGroup,
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    //点击菜单
    clickMenu(item) {
      if (
        this.$route.path !== item.path &&
        !(this.$route.path === "/home" && item.path === "/")
      ) {
        // this.$router.push({
        // path: item.path,
        // name:item.name, //或
        // params:{},
        // query:{}
        // });
        this.$router.push(item.path);
      }
      this.$store.commit("selectMenu", item);

      //console.log(item);
    },
  },
  computed: {
    //没有子菜单
    noChildren() {
      return this.menuData.filter((item) => !item.children);
    },
    //有子菜单
    hasChildren() {
      return this.menuData.filter((item) => item.children);
    },
    menuData() {
      //判断当前数据，如果缓存中没有，当前store中去获取
      return (
        JSON.parse(localStorage.getItem("menu")) || this.$store.state.tab.menu
      );
    },
    isCollapse() {
      return this.$store.state.tab.isCollapse;
      //读取tab.isCollapse里面的vuex中的数据
    },
  },
};
</script>